package com.example.weight;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.weight.adapter.Course;
import com.handmark.pulltorefresh.library.recycler.RecyclerTouchListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PullRecyclerViewActivity extends AppCompatActivity {
    @BindView(R.id.iv_back)
    ImageView mBackView;
    @BindView(R.id.tv_edit)
    TextView mEditView;
    @BindView(R.id.rcv_course)
    PullToRefreshRecyclerView mCourseListView;
    private int mPage = 1;
    private List<Course> mCourseData;
    private CourseAdapter mCourseAdapter;
    private RecyclerTouchListener mRecyclerTouchListener;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentId());
        ButterKnife.bind(this);
        initData();
        initView(savedInstanceState);

    }

    private void initData(){
        mCourseData = new ArrayList<>();
        mCourseData.add(new Course());
    }

    private void loadMoreData(){
        for(int i = 0 ; i < mPage; i++){
            mCourseData.add(new Course());
        }
    }

    public void showToast(String msg){
        if (null==msg||msg.equals(""))
            return;
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void startActivity(Class c){
        startActivity(new Intent(this,c));
    }

    public void startActivity(Class c,String iKey,String iValue){
        Intent it = new Intent(this,c);
        it.putExtra(iKey,iValue);
        startActivity(it);
    }

    public void startActivity(Class c, Bundle data){
        Intent it = new Intent(this,c);
        it.putExtras(data);
        startActivity(it);
    }

    protected int getContentId() {
        return R.layout.activity_recycler;
    }

    protected void initView(Bundle savedInstanceState) {
        initCourseListView();
    }

    @OnClick({R.id.iv_back, R.id.tv_edit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_edit:
                new Handler().postDelayed(new Runnable(){
                    public void run(){
                        mCourseListView.setRefreshing();
                    }
                },200);
                updateEditViewState();
                break;
        }
    }

    private void updateEditViewState(){
        if(mEditView.getTag() == null){
            mEditView.setTag(false);
        }
        boolean selected = !(boolean) mEditView.getTag();
        mEditView.setTag(selected);
        mEditView.setText(selected ? "完成" : "编辑");
        mEditView.setBackgroundResource(selected ? R.drawable.bg_circle_range : R.drawable.bg_range);
        if(selected){
            mRecyclerTouchListener.closeVisibleBG();
        }
        mRecyclerTouchListener.setEnabled(!selected);
        mCourseListView.setMode(selected ? PullToRefreshBase.Mode.DISABLED : PullToRefreshBase.Mode.BOTH);
    }


    //初始化数据列表及刷新设置
    private void initCourseListView() {
        RecyclerView contentView = (RecyclerView) mCourseListView.getRefreshableView();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        contentView.setLayoutManager(layoutManager);
        contentView.setAdapter(mCourseAdapter = new CourseAdapter(this, mCourseData));

        mCourseListView.setMode(PullToRefreshBase.Mode.BOTH);

        mRecyclerTouchListener = new RecyclerTouchListener(this, (RecyclerView) mCourseListView.getRefreshableView());
        mRecyclerTouchListener
                //设置item点击事件
                .setClickable(new RecyclerTouchListener.OnRowClickListener() {
                    @Override
                    public void onRowClicked(int position) {
                        // TODO: 2018/10/20 0020 item点击 处理数据时注意有foot栏，需处理相应逻辑
                        showToast("点击了第" + position + "项");
                    }

                    @Override
                    public void onIndependentViewClicked(int independentViewID, int position) {

                    }
                })
                //设置滑动显露的删除布局
                .setSwipeOptionViews(R.id.iv_delete)
                .setSwipeable(R.id.rl_content, R.id.rl_delete, new RecyclerTouchListener.OnSwipeOptionsClickListener() {
                    @Override
                    public void onSwipeOptionClicked(int viewID, int position) {
                        if(viewID == R.id.iv_delete){
                            // TODO: 2018/10/20 0020 删除操作
                            showToast("删除 ： " + position);
                        }
                    }
                });

    }

    @Override
    protected void onResume() {
        super.onResume();
        ((RecyclerView)mCourseListView.getRefreshableView()).addOnItemTouchListener(mRecyclerTouchListener);
    }

    @Override
    protected void onPause() {
        super.onPause();
        ((RecyclerView)mCourseListView.getRefreshableView()).removeOnItemTouchListener(mRecyclerTouchListener);
    }

    class CourseAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private Context mContext;
        private List<Course> mCourses;

        CourseAdapter(Context context, List<Course> courses) {
            mContext = context;
            mCourses = courses;
        }

        void initData(List<Course> courses) {
            mCourses = courses;
            notifyDataSetChanged();
        }

        void addData(List<Course> courses) {
            if (mCourses == null) {
                mCourses = new ArrayList<>();
            }
            mCourses.addAll(courses);
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            return new CourseAdapter.CourseHolder(LayoutInflater.from(mContext).inflate(R.layout.item_collect_course, null));
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {

        }

        @Override
        public int getItemCount() {
            return mCourses == null ? 0 : mCourses.size();
        }

        class CourseHolder extends RecyclerView.ViewHolder {
            CourseHolder(@NonNull View itemView) {
                super(itemView);
            }
        }
    }
}
